package com.epfresh.api.widget.gradient.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

import com.epfresh.api.widget.gradient.inter.GradientStyle;
import com.epfresh.api.widget.gradient.inter.Gradually;
import com.epfresh.api.widget.gradient.inter.SyncGradually;

/**
 * 渐变ActionBar
 *
 * @author 陈小默
 */
public class GradientActionBar extends RelativeLayout implements Gradually {
    private int red = 0xff;
    private int blue = 0xff;
    private int green = 0xff;
    private SyncGradually scrollView;
    private int gradientStyle = 0;

    public GradientActionBar(Context context, AttributeSet attrs,
                             int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public GradientActionBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GradientActionBar(Context context) {
        this(context, null);
    }

    @Override
    public void setGrade(int grade) {
        this.setBackgroundColor(Color.argb(
                (gradientStyle == GradientStyle.DESC ? grade : 0xff - grade),
                red, green, blue));
    }

    @Override
    public void setGradeColor(int red, int green, int blue) {
        this.setBackgroundColor(Color.argb(
                (gradientStyle == GradientStyle.DESC ? 0xff : 0),
                this.red = red, this.green = green, this.blue = blue));
    }

    /**
     * 绑定方法
     *
     * @param scrollView
     */
    public void bind(SyncGradually scrollView, int style) {
        this.scrollView = scrollView;
        this.gradientStyle = style;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (scrollView != null)
            scrollView.syncDimention(this.getHeight(), this.getWidth());
    }
}
